package com.ysd.lis.service.eq.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.SysUser;
import com.ysd.lis.entity.WkFolder;
import com.ysd.lis.entity.eq.*;
import com.ysd.lis.mapper.eq.EqFaultRepairMapper;
import com.ysd.lis.mapper.eq.EqRepairOrderMapper;
import com.ysd.lis.mapper.eq.EqRepairRecordMapper;
import com.ysd.lis.request.CommonPageDto;
import com.ysd.lis.request.eq.EqRepairDto;
import com.ysd.lis.service.SysBasDictDetailService;
import com.ysd.lis.service.SysRuleSetService;
import com.ysd.lis.service.WkFolderService;
import com.ysd.lis.service.eq.EqFaultRepairService;
import com.ysd.lis.service.eq.EqRepairPersonService;
import com.ysd.util.RedisUserManager;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 设备管理-设备类型 服务实现类
 * </p>
 *
 * @author prs
 * @since 2024-08-26
 */
@Service
public class EqFaultRepairServiceImpl extends ServiceImpl<EqFaultRepairMapper, EqFaultRepair> implements EqFaultRepairService {

    @Autowired
    EqFaultRepairMapper eqFaultRepairMapper;
    @Autowired
    SysRuleSetService sysRuleSetService;
    @Autowired
    EqRepairOrderMapper eqRepairOrderMapper;
    @Autowired
    EqRepairPersonService eqRepairPersonService;
    @Autowired
    EqRepairRecordMapper eqRepairRecordMapper;
    @Autowired
    SysBasDictDetailService sysBasDictDetailService;
    @Override
    public Result findFaultRepairPageList(CommonPageDto pageParam) {
        Page<EqFaultRepair> page = new Page<>();
        page.setCurrent(pageParam.getPageIndex());
        page.setSize(pageParam.getPageSize());
        MPJLambdaWrapper<EqFaultRepair> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.selectAll(EqFaultRepair.class)
                .selectAs("eq", EqEquipment::getEquiptName,EqFaultRepair::getEquiptName)
                .selectAs("eq",EqEquipment::getSpecification,EqFaultRepair::getSpecification)
                .selectAs("ro",EqRepairOrder::getId,EqFaultRepair::getRepairId)
                .selectAs("ro",EqRepairOrder::getRepairNo,EqFaultRepair::getRepairNo)
                .selectAs("et",EqFaultType::getFaultTypeName,EqFaultRepair::getFaultTypeName)

                .leftJoin(EqEquipment.class,"eq",EqEquipment::getId,EqFaultRepair::getEquiptId)
                .leftJoin(EqRepairOrder.class,"ro",EqRepairOrder::getFaultId,EqFaultRepair::getId)
                .leftJoin(EqFaultType.class,"et",p->p.eq(EqFaultType::getFaultTypeCode,EqFaultRepair::getFaultType).eq(EqFaultType::getDelFlag,0) )

                .eq(EqFaultRepair::getDelFlag, 0);
        if (StringUtils.isNotBlank(pageParam.getSeachValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(EqFaultRepair::getFaultNo, pageParam.getSeachValue())
                    .or().like(EqFaultRepair::getEquiptCode, pageParam.getSeachValue()));
        }
        if(ToolsUtils.isNotEmpty(pageParam.getStatus())){
            lambdaQueryWrapper.eq(EqFaultRepair::getFaultStatus,pageParam.getStatus());
        }
        lambdaQueryWrapper.orderByDesc(EqFaultRepair::getCreateTime);
        Page<EqFaultRepair> orderPage = eqFaultRepairMapper.selectPage(page, lambdaQueryWrapper);
        return Result.succ(1, "查询成功", orderPage);
    }

    @Override
    public Result findEqFaultRepairById(EqFaultRepair eqFaultRepair) {
        String id = eqFaultRepair.getId();

        MPJLambdaWrapper<EqFaultRepair> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.selectAll(EqFaultRepair.class)
                .selectAs("eq",EqEquipment::getEquiptName,EqFaultRepair::getEquiptName)
                .selectAs("eq",EqEquipment::getSpecification,EqFaultRepair::getSpecification)
                .selectAs("et",EqFaultType::getFaultTypeName,EqFaultRepair::getFaultTypeName)
//                .selectAs("ro",EqRepairOrder::getId,EqFaultRepair::getRepairId)
//                .selectAs("ro",EqRepairOrder::getRepairNo,EqFaultRepair::getRepairNo)
                .leftJoin(EqEquipment.class,"eq",EqEquipment::getId,EqFaultRepair::getEquiptId)
                .leftJoin(EqFaultType.class,"et",p->p.eq(EqFaultType::getFaultTypeCode,EqFaultRepair::getFaultType).eq(EqFaultType::getDelFlag,0) )
//                .leftJoin(EqRepairOrder.class,"ro",EqRepairOrder::getFaultId,EqFaultRepair::getId)
                .eq(EqFaultRepair::getId,id)
                .eq(EqFaultRepair::getDelFlag, 0);
        EqFaultRepair faultRepair = eqFaultRepairMapper.selectOne(lambdaQueryWrapper);
        return Result.succ(1, "查询成功", faultRepair);
    }

    @Override
    public Result addFaultRepair(EqFaultRepair eqFaultRepair) {
        String a = sysRuleSetService.createRuleCodeList("eqFaultNo", 1).get(0);
        eqFaultRepair.setFaultNo(a);
        boolean save = this.save(eqFaultRepair);
        if (save) {
            //RepairOrder
            EqRepairOrder eqRepairOrder = new EqRepairOrder();
            String bb = sysRuleSetService.createRuleCodeList("eqRepairNo", 1).get(0);
            eqRepairOrder.setRepairNo(bb);
            eqRepairOrder.setFaultId(eqFaultRepair.getId());
            eqRepairOrder.setFaultNo(eqFaultRepair.getFaultNo());
            eqRepairOrder.setEquiptId(eqFaultRepair.getEquiptId());
            eqRepairOrder.setEquiptCode(eqFaultRepair.getEquiptCode());
            eqRepairOrder.setFaultDate(eqFaultRepair.getFaultDate());
            eqRepairOrder.setFaultOptor(eqFaultRepair.getFaultOptor());
            eqRepairOrder.setFaultType(eqFaultRepair.getFaultType());
            eqRepairOrder.setFaultGrade(eqFaultRepair.getFaultGrade());
            eqRepairOrder.setFaultRemark(eqFaultRepair.getFaultRemark());
            eqRepairOrder.setRepairStatus("0");
            eqRepairOrder.setSeq(1);
            eqRepairOrderMapper.insert(eqRepairOrder);
            //person
            List<EqRepairPerson> repairPersonList = new ArrayList<>();
            EqRepairPerson repairPerson = new EqRepairPerson();
            repairPerson.setRepairId(eqRepairOrder.getId());
            repairPerson.setRepairNo(eqRepairOrder.getRepairNo());
            repairPerson.setPersonId(eqFaultRepair.getPersonId());
            repairPerson.setPerson(eqFaultRepair.getFaultPerson());
            repairPerson.setFaultId(eqFaultRepair.getId());
            repairPersonList.add(repairPerson);
            boolean b = eqRepairPersonService.saveBatch(repairPersonList);
            //新增操作记录
            EqRepairRecord repairRecord = new EqRepairRecord();
            repairRecord.setRepairId(eqRepairOrder.getId());
            repairRecord.setRepairNo(eqRepairOrder.getRepairNo());
            repairRecord.setOperateDate(new Timestamp(new Date().getTime()));
            SysUser sysUser = RedisUserManager.getUser();
            repairRecord.setOperatorId(sysUser.getId());
            repairRecord.setOperator(sysUser.getRealname());
            repairRecord.setOperate("添加");
            repairRecord.setOperateResult("添加新故障，待维修");
            eqRepairRecordMapper.insert(repairRecord);

            //创建文件夹
            List<Map<String, Object>> fileFolderList = (List<Map<String, Object>>) sysBasDictDetailService.getSysBasDictList("eqFileFolder").getData();
            List<WkFolder> sbList = wkFolderService.list(new LambdaQueryWrapper<WkFolder>().eq(WkFolder::getCode, "eqFaultRepair").eq(WkFolder::getDelFlag, 0));
            if (CollectionUtil.isNotEmpty(sbList)) {
                WkFolder sbFolder = sbList.get(0);
                String[] split = sbFolder.getFileFolder().split(",");
                WkFolder wkFolder = new WkFolder();
                wkFolder.setCode(a);
                wkFolder.setName(a);
                wkFolder.setPid(sbFolder.getId());
                wkFolder.setGrade(0);
                wkFolderService.save(wkFolder);
                if (split.length > 0) {
                    for (String fileFolder : split) {
                        for (Map<String, Object> map : fileFolderList) {
                            if (fileFolder.equals(map.get("code"))) {
                                WkFolder wkFolder1 = new WkFolder();
                                wkFolder1.setCode(a+fileFolder);
                                wkFolder1.setName(map.get("name").toString());
                                wkFolder1.setPid(wkFolder.getId());
                                wkFolder1.setGrade(0);
                                wkFolderService.save(wkFolder1);
                            }
                        }
                    }
                }
            }
            return Result.succ(1, "添加成功", eqRepairOrder);

        } else {
            return Result.fail("添加失败");
        }
    }

    @Autowired
    WkFolderService wkFolderService;
    @Override
    public Result updatFaultRepair(EqFaultRepair eqFaultRepair) {
        return null;
    }

    @Override
    public Result batchDeleteFaultRepairList(EqRepairDto dto) {
        List<EqFaultRepair> eqFaultRepairList = dto.getEqFaultRepairList();
        List<String> ids = eqFaultRepairList.stream().map(EqFaultRepair::getId).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        if(b){
            //person
            return Result.succ(1, "删除成功", null);
        }else {
            return Result.fail(400,"删除失败","");
        }
    }
}
